# Contributor: Oleg Titov <oleg.titov@gmail.com>
# Maintainer: Oleg Titov <oleg.titov@gmail.com>
pkgname=bazel
pkgver=2.2.0
pkgrel=0
pkgdesc="A fast, scalable, multi-language and extensible build system"
url="https://bazel.build/"
arch="x86_64"
license="Apache-2.0"
depends="bash libarchive openjdk8 zip unzip"
makedepends="python3 linux-headers protobuf"
options="!strip"
subpackages="
	$pkgname-doc
	$pkgname-bash-completion
	$pkgname-zsh-completion
	$pkgname-examples::noarch
	"
source="https://github.com/bazelbuild/bazel/releases/download/$pkgver/bazel-$pkgver-dist.zip
	bash_completion.patch"

build() {
	# based on
	# https://github.com/vbatts/copr-build-bazel/blob/master/bazel.spec

	export JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk
	export EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk"

	# horrible of horribles, just to have python in the PATH
	# https://github.com/bazelbuild/bazel/issues/8665
	mkdir -p ./bin-hack
	ln -s /usr/bin/python3 ./bin-hack/python
	export PATH=$(pwd)/bin-hack:$PATH

	bash ./compile.sh

	./output/bazel shutdown

	./scripts/generate_bash_completion.sh \
		--bazel=output/bazel \
		--output=output/bazel-complete.bash \
		--prepend=scripts/bazel-complete-header.bash \
		--prepend=scripts/bazel-complete-template.bash

	echo startup --server_javabase=$JAVA_HOME \
		>> scripts/packages/bazel.bazelrc
}

check() {
	./output/bazel test \
		--verbose_failures \
		--spawn_strategy=standalone \
		--genrule_strategy=standalone \
		--verbose_test_summary \
		--test_verbose_timeout_warnings \
		examples/cpp:hello-success_test

	./output/bazel shutdown
}

package() {
	install -Dm755 ./scripts/packages/bazel.sh "$pkgdir"/usr/bin/bazel
	install -Dm755 ./output/bazel "$pkgdir"/usr/bin/bazel-real
	install -Dm755 ./scripts/packages/bazel.bazelrc \
		"$pkgdir"/etc/bazel.bazelrc

	mkdir -p "$pkgdir"/usr/share/bash-completion/completions
	mv "$builddir"/output/bazel-complete.bash \
		"$pkgdir"/usr/share/bash-completion/completions/bazel

	mkdir -p "$pkgdir"/usr/share/zsh/site-functions
	mv "$builddir"/scripts/zsh_completion/_bazel \
		"$pkgdir"/usr/share/zsh/site-functions/_bazel

	install -m644 -D -t "$pkgdir"/usr/share/doc/$pkgname README.md
}

examples() {
	mkdir -p "$subpkgdir"/usr/share/doc/"$pkgname"
	for d in examples third_party tools; do
		mv "$builddir/$d" "$subpkgdir/usr/share/doc/$pkgname"
	done
}

unpack() {
	mkdir -p "$builddir"
	unzip "$srcdir"/bazel-$pkgver-dist.zip -d "$builddir" > /dev/null
}

sha512sums="67011c3a045c0fd74cdcb98eef1c3ead6902a3bd65a629fe49911f9dbb4c39508bd91eb63c1fae68284136ad4994afb42a01c7f10983614e432f8be22a1c566e  bazel-2.2.0-dist.zip
6eb1fcb553518348f5c2dacf239377dbaa2593b751ba98c0c257d0138c55b159891fa3744168a6cff8b305758c64f9153cdc01dd3394b5fff2168319188288df  bash_completion.patch"
